/**
 * @a https://leetcode.cn/problems/sort-colors/
 */

#include "common.h"

class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int sz = nums.size();
        int left = 0, right = sz - 1;
        int i = 0;
        while (i <= right)
        {
            if (nums[i] == 0)
            {
                swap(nums[i++], nums[left++]);
                continue;
            }
            else if (nums[i] == 2)
            {
                swap(nums[i], nums[right--]);
                continue;
            }
            else
            {
                i++;
            }
        }
    }
};

class Solution
{
public:
    void sortColors(vector<int> &nums)
    {
        int sz = nums.size();
        int left = 0, right = sz - 1;
        for (int i = 0; i <= right;)
        {
            if (0 == nums[i])
            {
                swap(nums[i++], nums[left++]);
            }
            else if (2 == nums[i])
            {
                swap(nums[i], nums[right--]);
            }
            else
                ++i;
        }
    }
};